//
//  BaseNavView.swift
//  自定义导航栏
//
//  Created by mini on 2025/4/28.
//

import Foundation

class BaseNavView: UIView {
    
    var rightView: UIView!
    var leftView: UIView!
    var leftBlock: (() -> Void)?
    var rightBlock: (() -> Void)?
    var blockBack: (() -> Void)?
    
    // MARK: - lazy
    lazy var labelTitle: OutlineLabel = {
        let titleLabel = OutlineLabel()
        return titleLabel
    }()
    
    lazy var backBtn: UIControl = {
        let button = UIControl()
        button.addTarget(self, action: #selector(btnBackClick(_:)), for: .touchUpInside)
        BaseNavView.resetControl(button, imageName: "icon_nav_back", isLeft: true)
        return button
    }()
    
    lazy var lineBottom: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(SCREEN_WIDTH, 5)
        result.image = UIImage(named: "icon_nav_bottom")
        return result
    }()

    // MARK: - 初始化
    required init() {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: NAVIGATIONBAR_HEIGHT+5))
        self.backgroundColor = COLOR_PRIMARY()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 刷新View
    func resetNavTitle(title: String, leftView: UIView?, rightView: UIView?) {
        labelTitle.resetNavTitle(title: title)
        self.labelTitle.centerX = self.width/2
        self.labelTitle.centerY = (NAVIGATIONBAR_HEIGHT - STATUSBAR_HEIGHT)/2.0 + STATUSBAR_HEIGHT;
        self.lineBottom.leftBottom = XY(0, NAVIGATIONBAR_HEIGHT+5)
        self.addSubview(self.labelTitle)
        //reset left view
        self.resetNavLeftView(view: leftView)
        //reset right view
        self.resetNavRightView(view: rightView)
        self.addSubview(lineBottom)
    }
    
    func resetNavLeftView(view: UIView?) {
        if let markView = view {
            self.leftView = markView
            markView.frame = CGRectMake(0, STATUSBAR_HEIGHT, leftView.width, NAVIGATIONBAR_HEIGHT-STATUSBAR_HEIGHT)
            addSubview(markView)
        }
    }
    
    func resetNavRightView(view: UIView?) {
        if let markView = view {
            self.rightView = markView
            markView.frame = CGRectMake(SCREEN_WIDTH-rightView.width, STATUSBAR_HEIGHT, rightView.width, NAVIGATIONBAR_HEIGHT-STATUSBAR_HEIGHT)
            addSubview(markView)
        }
    }
    
    // MARK: - 左view 右view
    class func initNavTitle(title: String, leftView: UIView?, rightView: UIView?) -> Self {
        let baseNav = self.init()
        baseNav.resetNavTitle(title: title, leftView: leftView, rightView: rightView)
        return baseNav
    }
    
    class func initNavIsBoldTitle(title: String, leftView: UIView?, rightView: UIView?) -> Self {
        let baseNav = self.init()
        baseNav.resetNavTitle(title: title, leftView: leftView, rightView: rightView)
        return baseNav
    }

    // MARK: - 左返回 右view
    class func initNavBackTitle(title: String, rightView: UIView?) -> Self {
        let baseNav = self.init()
        baseNav.resetNavBackTitle(title: title, rightView: rightView)
        return baseNav;
    }
    
    func resetNavBackTitle(title: String, rightView: UIView?) {
        self.resetNavTitle(title: title, leftView: self.backBtn, rightView: rightView)
    }
    
    // MARK: - 左图片 右图片
    class func initNavTitle(title: String, leftImageName: String?, leftBlock: (() -> Void)?, rightImageName: String?, rightBlock: (() -> Void)?) -> Self {
        let baseNav = self.init()
        baseNav.resetNavTitle(title: title, leftImageName: leftImageName, leftBlock: leftBlock, rightImageName: rightImageName, rightBlock: rightBlock)
        return baseNav;
    }
    
    func resetNavTitle(title: String, leftImageName: String?, leftBlock: (() -> Void)?, rightImageName: String?, rightBlock: (() -> Void)?) {
        var leftView: UIView?
        if (isStr(leftImageName)) {
            self.leftBlock = leftBlock
            let con = UIControl()
            BaseNavView.resetControl(con, imageName: leftImageName!, isLeft: true)
            con.addTarget(self, action: #selector(btnLeftClick(_ :)), for: .touchUpInside)
            leftView = con;
        }
        var rightView: UIView?
        if (isStr(rightImageName)) {
            self.rightBlock = rightBlock
            let con = UIControl()
            BaseNavView.resetControl(con, imageName: rightImageName!, isLeft: false)
            con.addTarget(self, action: #selector(btnRightClick(_ :)), for: .touchUpInside)
            rightView = con;
        }
        self.resetNavTitle(title: title, leftView: leftView, rightView: rightView)
    }
    
    // MARK: - 左返回 右图片
    class func initNavBackWithTitle(title: String, rightImageName: String?, rightBlock: (() -> Void)?) -> Self {
        let baseNav = self.init()
        baseNav.resetNavBackWithTitle(title: title, rightImageName: rightImageName, rightBlock: rightBlock)
        return baseNav;
    }
    
    func resetNavBackWithTitle(title: String, rightImageName: String?, rightBlock: (() -> Void)?){
        self.rightBlock = rightBlock
        let control = UIControl.init()
        BaseNavView.resetControl(control, imageName: rightImageName ?? "", isLeft: false)
        control.addTarget(self, action: #selector(btnRightClick(_ :)), for: .touchUpInside)
        self.resetNavTitle(title: title, leftView: self.backBtn, rightView: control)
    }
    
    // MARK: - 左返回 右文字
    class func initNavBackTitle(title: String, rightTitle: String?, rightBlock: (() -> Void)?) -> Self {
        let baseNav = self.init()
        baseNav.initNavBackTitle(title: title, rightTitle: rightTitle, rightBlock: rightBlock)
        return baseNav
    }
    
    func initNavBackTitle(title: String, rightTitle: String?, rightBlock: (() -> Void)?){
        self.rightBlock = rightBlock;
        if (!isStr(rightTitle)){
            self.resetNavTitle(title: title, leftView: self.backBtn, rightView: nil)
            return;
        }
        let con = UIControl.init()
        BaseNavView.resetControl(con, title: title, isLeft: false)
        con.addTarget(self, action: #selector(btnRightClick(_ :)), for: .touchUpInside)
        self.resetNavTitle(title: title, leftView: self.backBtn, rightView: con)
    }
    
    // MARK: - 更改title
    func changeTitle(title: String) {
        labelTitle.resetNavTitle(title: title)
        self.labelTitle.centerX = self.width/2
    }

    // MARK: - 更改Nav right title
    func changeRightTitle(rightTitle: String) {
        self.rightView.removeFromSuperview()
        let con = UIControl.init()
        BaseNavView.resetControl(con, title: rightTitle, isLeft: false)
        con.addTarget(self, action: #selector(btnRightClick(_ :)), for: .touchUpInside)
        self.resetNavRightView(view: con)
    }

    // MARK: - 更改Nav right image
    func changeRightImage(imageName: String) {
        self.rightView.removeFromSuperview()
        let con = UIControl.init()
        BaseNavView.resetControl(con, imageName: imageName, isLeft: false)
        con.addTarget(self, action: #selector(btnRightClick(_ :)), for: .touchUpInside)
        self.resetNavRightView(view: con)
    }
    
    // MARK: - 设置图片
    static func resetControl(_ control: UIView, imageName: String, isLeft: Bool) {
        control.backgroundColor = .clear
        control.subviews.forEach { $0.removeFromSuperview() }
        let iv = UIImageView(image: UIImage(named: imageName))
        iv.backgroundColor = .clear
        if isLeft {
            control.frame = CGRectMake(0, STATUSBAR_HEIGHT, W(100), NAVIGATIONBAR_HEIGHT-STATUSBAR_HEIGHT)
            iv.left = W(15)
        } else {
            control.frame = CGRectMake(SCREEN_WIDTH - W(100), STATUSBAR_HEIGHT, W(100), NAVIGATIONBAR_HEIGHT - STATUSBAR_HEIGHT)
            iv.right = control.width - W(15)
        }
        iv.centerY = control.height / 2.0
        control.addSubview(iv)
    }
    
    // MARK: - 设置文字
    static func resetControl(_ control: UIView, title: String, isLeft: Bool) {
        control.backgroundColor = .clear
        control.subviews.forEach { $0.removeFromSuperview() }
        let label = UILabel.init()
        label.numLimit = 6;
        label.numberOfLines = 1;
        label.textColor = COLOR_WHITE();
        label.fontNum = 13;
        label.backgroundColor = .clear;
        label.fitTitle(title, variable: 0)
        if isLeft {
            control.frame = CGRectMake(0, STATUSBAR_HEIGHT, W(100), NAVIGATIONBAR_HEIGHT-STATUSBAR_HEIGHT)
            label.left = W(20)
        } else {
            control.frame = CGRectMake(SCREEN_WIDTH - W(100), STATUSBAR_HEIGHT, W(100), NAVIGATIONBAR_HEIGHT - STATUSBAR_HEIGHT)
            label.right = control.width - W(15)
        }
        label.centerY = control.height / 2.0
        control.addSubview(label)
    }
    
    // MARK: - 添加阴影
    func resetNavAddShadow() {
        self.layer.shadowOffset = CGSizeMake(3, 3)
        self.layer.shadowColor = UIColor(hex: "EEEEEE").cgColor
        self.layer.shadowOpacity = 1
    }

    // MARK: - 设置白色模式
    func resetWhiteStyle() {
        labelTitle.labelTitle.textColor = COLOR_WHITE()
        BaseNavView.resetControl(self.backBtn, imageName: "icon_nav_back", isLeft: true)
    }

    // MARK: - 点击事件
    @objc func btnBackClick(_ sender: UIControl) {
        feedback()
        if ((self.blockBack) != nil) {
            self.blockBack!();
        }else{
            self.popVC()
        }
    }
    
    func popVC() {
        if let vcRespond = fetchVC(), let blockWillBack = vcRespond.blockWillBack {
            blockWillBack(vcRespond);
        }else{
            GB_Nav?.popViewController(animated: true)
        }
    }
    
    @objc func btnRightClick(_ sender: UIControl) {
        feedback()
        if let rightBlock = rightBlock {
            rightBlock()
        }
    }

    @objc func btnLeftClick(_ sender: UIControl) {
        feedback()
        if let leftBlock = leftBlock {
            leftBlock()
        }
    }
}
